<template>
	<view>
		<!-- 分类导航选项卡 -->
		<view class="tag" :class="isScrolled ? 'tag-scroll' : ''">
			<u-tabs 
				:list="splist" 
				:height="80" 
				name="categoryName"  
				:active-color="'#EF7B3A'"	
				:is-scroll="true" 
				:current="current" 
				@change="handleTabChange" 
				style="width: 100%;"
				:transition="tabTransition"
			></u-tabs>
		</view>
		
		<view class="list">
			<!-- 左侧区域和楼层选择（高度自适应优化） -->
			<view class="list_left_wrapper">
				<view class="list_left" ref="leftContainer">
					<!-- 固定的标题 -->
					<!-- <view class="left_title">区域楼层</view> -->
					
					<!-- 区域列表 -->
					<view class="left_scroll_content">
						<view 
							class="left_itme" 
							v-for="(item, index) in bcroylist" 
							:key="index"
							:class="{ 'fade-in': index < 5 }"
						>
							<view 
								class="item_name" 
								:style="{'max-width': getMaxWidth()}"
							>{{item.areaName}}</view>
							<view 
								class="colist" 
								v-for="(it, idex) in item.floors" 
								:key="idex" 
								@click="Selectarea(index, idex, it.floorId)"
								:class="{ 'selected-floor': asndex == index && astdex == idex }"
								:style="{ 'animation-delay': `${(index * 0.1 + idex * 0.05)}s` }"
							>
								<view class="name">{{it.floorName}}</view>
							</view>
						</view>
					</view>
				</view>
			</view>
			
			<!-- 右侧品牌列表 -->
			<view class="list_rigt">
				<!-- 数据为空时显示暂无数据提示 -->
				<view class="no-data" v-if="barnd_list.length === 0 && !loading && reust">
					<image src="@/static/no-data.png" mode="aspectFit"></image>
					<text class="no-data-text">暂无相关数据</text>
				</view>
				
				<!-- 数据列表 -->
				<view 
					class="barnd_item" 
					v-for="(item, index) in barnd_list" 
					:key="index"
					:class="{ 'fade-in': index < 10, 'loaded': index >= 10 }"
					@load="handleImageLoad(index)"
					@click="BandNavurl(item.id)"
				>
					<view class="barnd_img">
						<image 
							:src="'https:' + item.logo" 
							mode=""
							:class="{ 'scale-in': imageLoaded[index] }"
						></image>
					</view>
					<view class="name">{{item.name}}</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { shopCategorylist, mallArealist, shoppage } from '@/core/api/index.js'
	export default {
		data() {
			return {
				splist: [
					{
						categoryName:'全部'
					}
				],
				current: 0,
				bcroylist: [],
				asndex: -1,
				astdex: -1,
				barnd_list: [],
				isScrolled: false,
				tabTransition: 'slide',
				imageLoaded: {},
				loading: false,
				reust:false,
				currentCategoryId: '', // 当前选中的分类ID
				currentAreaId: '',     // 当前选中的区域ID
				currentFloorId: ''     // 当前选中的楼层ID
			}
		},
		onLoad() {
			this.initPage()
			this.setupScrollListener()
		},
		onUnload() {
			uni.removeEventListener('scroll', this.handleScroll)
		},
		methods: {
			BandNavurl(id){
				uni.navigateTo({
					url:`/pages/brand/brand_details?id=${id}`
				})
			},
			// 初始化页面数据
			async initPage() {
				try {
					await this.fetchCategoryList()
					await this.fetchMallAreas()
					await this.fetchShopListByCombination()
				} catch (error) {
					console.error('数据获取失败:', error)
					uni.showToast({
						title: '数据加载失败',
						icon: 'none'
					})
				}
			},
			
			// 设置滚动监听
			setupScrollListener() {
				uni.addEventListener('scroll', this.handleScroll)
			},
			
			// 处理滚动事件
			handleScroll() {
				this.isScrolled = uni.getSystemInfoSync().scrollTop > 50
			},
			
			// 选项卡切换处理
			handleTabChange(e) {
				this.current = e
				this.tabTransition = this.tabTransition === 'slide' ? 'fade' : 'slide'
				if(this.current == 0){
					this.asndex = -1
				    this.currentCategoryId = ''
					this.currentAreaId = ''
					this.currentFloorId = ''
				}
				// 记录当前分类ID并触发联合查询
				if (this.splist.length > e) {
					this.currentCategoryId = this.splist[e].id;
					this.fetchShopListByCombination();
				}
			},
			
			// 选择区域和楼层
			Selectarea(index, i, id) {
				this.asndex = index
				this.astdex = i
				
				// 记录当前区域和楼层ID并触发联合查询
				if (this.bcroylist[index] && this.bcroylist[index].floors[i]) {
					this.currentAreaId = this.bcroylist[index].areaId;
					this.currentFloorId = id;
					this.fetchShopListByCombination();
				}
			},
			
			// 分类与区域联合查询
			async fetchShopListByCombination() {
				this.loading = true
				try {
					// 构建组合查询参数
					let params = {
						page: 1,
						size: 1000,
						condition: {
							categoryId: this.currentCategoryId,
							areaId: this.currentAreaId,
							floorId: this.currentFloorId
						}
					}
					
					const { data } = await shoppage(params)
					console.log(data)
					this.barnd_list = data.records || []
					this.reust = true
				} catch (error) {
					console.error('获取店铺数据失败:', error)
					this.barnd_list = []
				} finally {
					this.loading = false
				}
			},
			
			// 获取分类列表
			async fetchCategoryList() {
				try {
					const { data } = await shopCategorylist()
					this.splist = [...this.splist , ...data]
				} catch (error) {
					console.error('获取分类列表失败:', error)
				}
			},
			
			// 获取商场区域列表
			async fetchMallAreas() {
				try {
					const { data } = await mallArealist()
					this.bcroylist = data
				} catch (error) {
					console.error('获取商场区域失败:', error)
				}
			},
			
			// 处理图片加载
			handleImageLoad(index) {
				this.imageLoaded[index] = true
			},
			
			// 获取最大宽度
			getMaxWidth() {
				return this.$refs.leftContainer ? this.$refs.leftContainer.clientWidth - 40 + 'rpx' : '140rpx'
			}
		}
	}
</script>

<style>
	page {
		background: #F8F8F8;
		transition: background-color 0.3s ease;
	}
	
	/* 暂无数据样式 */
	.no-data {
		width: 100%;
		height: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 40rpx;
	}
	
	.no-data image {
		width: 240rpx;
		height: 240rpx;
		margin-bottom: 30rpx;
	}
	
	.no-data-text {
		font-size: 28rpx;
		color: #999999;
	}
</style>

<style lang="scss">
/* 基础动画定义 */
@keyframes fadeIn {
	from { opacity: 0; transform: translateY(10px); }
	to { opacity: 1; transform: translateY(0); }
}

@keyframes scaleIn {
	from { transform: scale(0.8); opacity: 0; }
	to { transform: scale(1); opacity: 1; }
}

@keyframes tabSlide {
	from { transform: translateX(-10px); opacity: 0; }
	to { transform: translateX(0); opacity: 1; }
}

@keyframes tabFade {
	from { opacity: 0; }
	to { opacity: 1; }
}

.fade-in {
	animation: fadeIn 0.5s ease forwards;
	opacity: 0;
}

.scale-in {
	animation: scaleIn 0.3s ease forwards;
}

.loaded {
	animation-delay: 0s !important;
}

/* 选项卡样式优化 */
.tag {
	height: 100rpx;
	background-color: #fff;
	position: sticky;
	top: 0;
	z-index: 100;
	box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
	transition: all 0.3s ease;
}

.tag-scroll {
	height: 80rpx;
	background-color: rgba(255, 255, 255, 0.95);
	box-shadow: 0 2rpx 15rpx rgba(0, 0, 0, 0.1);
}

/* 左侧区域选择样式（高度自适应优化） */
.list {
	display: flex;
}

.list_left_wrapper {
	/* 固定左侧区域但不限制高度 */
	position: sticky;
	top: 100rpx; /* 与顶部选项卡高度一致 */
	width: 23%;
	/* 移除固定高度限制，改为最大高度限制 */
	max-height: calc(100vh - 100rpx);
	overflow: hidden;
	z-index: 90;
}

.list_left {
	width: 100%;
	background-color: #FBE0CF;
	border-radius: 0 0 20rpx 20rpx;
	display: flex;
	flex-direction: column;
}

/* 左侧固定标题 */
.left_title {
	height: 80rpx;
	line-height: 80rpx;
	text-align: center;
	font-size: 32rpx;
	font-weight: 500;
	color: #F79D2F;
	background-color: #FBE0CF;
	border-bottom: 1px solid rgba(247, 157, 47, 0.1);
	z-index: 10;
}

/* 左侧滚动内容区域（核心：自适应高度） */
.left_scroll_content {
	/* 内容少时，高度由内容撑开 */
	/* 内容多时，高度限制为最大可用空间并启用滚动 */
	max-height: calc(100vh - 180rpx); /* 减去顶部选项卡和左侧标题高度 */
	overflow-y: auto;
	overflow-x: hidden;
	padding: 0 20rpx;
	
	/* 滚动条美化 */
	&::-webkit-scrollbar {
		width: 6rpx;
	}
	&::-webkit-scrollbar-track {
		background: rgba(255, 255, 255, 0.2);
		border-radius: 10rpx;
	}
	&::-webkit-scrollbar-thumb {
		background: rgba(247, 157, 47, 0.3);
		border-radius: 10rpx;
	}
	&::-webkit-scrollbar-thumb:hover {
		background: rgba(247, 157, 47, 0.5);
	}
}

.left_itme {
	padding: 10rpx 0;
	
	.item_name {
		margin: 25rpx auto;
		height: 52rpx;
		background-color: #F79D2F;
		text-align: center;
		line-height: 50rpx;
		color: #fff;
		border-radius: 25rpx;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
		font-size: 30rpx;
		transition: all 0.2s ease;
		padding: 0 15rpx;
		min-width: 120rpx;
		
		&:hover {
			transform: scale(1.02);
			box-shadow: 0 4rpx 10rpx rgba(247, 157, 47, 0.3);
		}
	}
	
	.colist {
		margin: 10rpx 0;
		transition: all 0.3s ease;
		
		&:hover {
			transform: translateX(5rpx);
		}
		
		.name {
			text-align: center;
			font-size: 28rpx;
			color: #7C7875;
			padding: 12rpx 20rpx;
			border-radius: 20rpx;
			transition: all 0.3s ease;
		}
	}
}

/* 选中的楼层样式 */
.selected-floor {
	.name {
		color: #F79D2F;
		background: #fff;
		border-radius: 20rpx;
		box-shadow: 0 4rpx 12rpx rgba(247, 157, 47, 0.2);
	}
}

/* 右侧品牌列表样式 */
.list_rigt {
	flex: 1;
	display: flex;
	height: 100%;
	flex-wrap: wrap;
	padding: 40rpx 0rpx;
	
	.barnd_item {
		width: 24.33%;
		margin-left: 40rpx;
		margin-bottom: 40rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		
		.barnd_img {
			width: 100%;
			height: 140rpx;
			border-radius: 10rpx;
			overflow: hidden;
			box-shadow: 0 6rpx 16rpx rgba(0, 0, 0, 0.08);
			
			image {
				width: 100%;
				height: 100%;
				transition: transform 0.5s ease;
			}
			
			&:hover image {
				transform: scale(1.05);
			}
		}
		
		color: #818181;
		font-size: 26rpx;
		text-align: center;
		.name {
			margin-top: 16rpx;
			white-space: nowrap;
			overflow: hidden;
			text-overflow: ellipsis;
			max-width: 100%;
		}
	}
}
</style>
